package com.xoa.service.party;

import com.xoa.dao.party.ArchivesMapper;
import com.xoa.model.party.Archives;
import com.xoa.model.party.ArchivesExample;
import com.xoa.model.party.ArchivesWithBLOBs;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by qiyang on 2019/4/30.
 */
@Service
public class ArchivesService {

    @Autowired
    private ArchivesMapper archivesMapper;

    //查询所有
    public List<ArchivesWithBLOBs> findAll(ArchivesExample example){
        return archivesMapper.selectByExampleWithBLOBs(example);
    }

    public List<ArchivesWithBLOBs> findAll(Integer page, Integer pageSize, Boolean useFlag, Map map){

        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page", pageParams);
        return archivesMapper.findAll(map);
    }
    //添加
    public ToJson<Archives> insert(HttpServletRequest request , ArchivesWithBLOBs WithBLOBs){
        ToJson<Archives> toJson = new ToJson<>(1, "error");
        try {
            Integer cus = archivesMapper.insert(WithBLOBs);
            if (cus > 0) {
                toJson.setMsg("ok");
                toJson.setFlag(0);
            } else {
                toJson.setMsg("添加失败");
                toJson.setFlag(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            // L.e("TimeLineServiceImpl deleteByUuid:" + e);
        }
        return toJson;
    }

    //修改
    public ToJson<Archives> update(HttpServletRequest request , ArchivesWithBLOBs archives){
        ToJson<Archives> toJson = new ToJson<>(1, "error");
        try {
            Integer cus = archivesMapper.updateByPrimaryKeyWithBLOBs(archives);
            if (cus > 0) {
                toJson.setMsg("ok");
                toJson.setFlag(0);
            } else {
                toJson.setMsg("编辑失败");
                toJson.setFlag(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //根据id回显
    public Archives selectByPrimaryKey(Integer archivesId){
        return archivesMapper.selectByPrimaryKey(archivesId);
    }

    //删除
    public ToJson<Archives> delete(HttpServletRequest request , Integer archivesId){
        ToJson<Archives> toJson = new ToJson<>(1, "error");

        try {
            int a =  archivesMapper.deleteByPrimaryKey(archivesId);
            if (a > 0) {
                toJson.setMsg("ok");
                toJson.setFlag(0);
            } else {
                toJson.setMsg("删除失败");
                toJson.setFlag(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());

        }
        return toJson;
    }
}
